jQuery(document).ready(function($) {
  // Get header slide up to make it slide down later.
  $(".description").slideUp();
  /* Navigation */
  $(window).resize(function()
  {
    if ($(window).width() >= 765) {
      $(".sidebar #nav").slideDown(350);
    }
    else {
      $(".sidebar #nav").slideUp(350);
    }
  });

  $("#nav > li > a").click(function(e) {
    if ($(this).parent().hasClass("has_sub")) {
      e.preventDefault();
    }

    if (!$(this).hasClass("subdrop")) {
      // hide any open menus and remove all other classes
      $("#nav li ul").slideUp(350);
      $("#nav li a").removeClass("subdrop");

      // open our new menu and add the open class
      $(this).next("ul").slideDown(350);
      $(this).addClass("subdrop");
    }

    else if ($(this).hasClass("subdrop")) {
      $(this).removeClass("subdrop");
      $(this).next("ul").slideUp(350);
    }

  });
  $("#nav > li > ul > li > a").click(function(e) {
    if ($(this).parent().hasClass("has_sub")) {
      e.preventDefault();
    }

    if (!$(this).hasClass("subdrop")) {
      // hide any open menus and remove all other classes
      $("#nav li ul li ul").slideUp(350);
      $("#nav li ul li  a").removeClass("subdrop");

      // open our new menu and add the open class
      $(this).next("ul").slideDown(350);
      $(this).addClass("subdrop");
    }

    else if ($(this).hasClass("subdrop")) {
      $(this).removeClass("subdrop");
      $(this).next("ul").slideUp(350);
    }
  });

  /* Contact us Slider */
  $(".symbol").click(function(e) {
    e.preventDefault();
    if (!$(".area").hasClass("open")) {
      $(".description").slideDown(300);
      $(".area").addClass("open");
      //$(".social-links").fadeOut();
      $(this).css("background-position", "-42px 0");
    }

    else if ($(".area").hasClass("open")) {
      $(".description").slideUp(300);
      $(".area").removeClass("open");
      //$(".social-links").fadeIn();
      $(this).css("background-position", "0 0");
    }
  });

   $(".sidebar-dropdown a").click(function(e) {
    e.preventDefault();

    if (!$(this).hasClass("open")) {
      // hide any open menus and remove all other classes
      $(".sidebar #nav").slideUp(350);
      $(".sidebar-dropdown a").removeClass("open");

      // open our new menu and add the open class
      $(".sidebar #nav").slideDown(350);
      $(this).addClass("open");
    }

    else if ($(this).hasClass("open")) {
      $(this).removeClass("open");
      $(".sidebar #nav").slideUp(350);
    }
  });
  
  $('div#block-contact_form_blocks-0 #edit-submit').click(function() {
    var errorFlag = false;
    if ($('div#block-contact_form_blocks-0 #edit-name').val().length == 0) {
      $('div#block-contact_form_blocks-0 #edit-name').addClass('error');
      errorFlag = true;
    }
    if ($('div#block-contact_form_blocks-0 #edit-mail').val().length == 0) {
      $('div#block-contact_form_blocks-0 #edit-mail').addClass('error');
      errorFlag = true;
    }
    if ($('div#block-contact_form_blocks-0 #edit-message').val().length == 0) {
      $('div#block-contact_form_blocks-0 #edit-message').addClass('error');
      errorFlag = true;
    }
    if (errorFlag) {
      alert('Oops! Something went wrong, Please rectify errors.')
      return false;
    }
  });
});

/* Recent posts carousel */
jQuery(window).load(function() {
  jQuery('.rps-carousel').carouFredSel({
    responsive: true,
    width: '100%',
    pauseOnHover: true,
    auto: false,
    circular: true,
    infinite: false,
    prev: {
      button: "#car_prev",
      key: "left",
    },
    next: {
      button: "#car_next",
      key: "right",
    },
    swipe: {
      onMouse: true,
      onTouch: true
    },
    items: {
      visible: {
        min: 1,
        max: 3
      }
    }
  });
  
  jQuery('.flexslider').flexslider({
    animation: "fade",
    controlNav: false,
    pauseOnHover: true,
    slideshowSpeed: 5000,
    animationSpeed: 2000
  });
  
});

/* Support */
jQuery("#slist a").click(function(e) {
  e.preventDefault();
  $(this).next('p').toggle(200);
});


/* Home widget */
jQuery('.slide-box-button').click(function() {
  var $slidebtn = $(this);
  var $slidebox = $(this).parent().parent();
  if ($slidebox.css('right') == "-252px") {
    $slidebox.animate({
      right: 0
    }, 500);
    $slidebtn.children("i").removeClass().addClass("icon-chevron-right");
  }
  else {
    $slidebox.animate({
      right: -252
    }, 500);
    $slidebtn.children("i").removeClass().addClass("icon-chevron-left");
  }
});

/* Tab */
jQuery('#myTab a').click(function(e) {
  e.preventDefault();
  $(this).tab('show');
})

/* Coming soon page twitter */
jQuery(function($) {
  $(".ctweet").tweet({
    username: "webivorous",
    join_text: "auto",
    avatar_size: 0,
    count: 1,
    auto_join_text_default: "we said,",
    auto_join_text_ed: "we",
    auto_join_text_ing: "we were",
    auto_join_text_reply: "we replied to",
    auto_join_text_url: "we were checking out",
    loading_text: "loading tweets...",
    template: "{text}"
  });
});

/* prettyPhoto Gallery */

jQuery(".prettyphoto").prettyPhoto({
  overlay_gallery: false, social_tools: false
});


/* Isotype */

// cache container
var $container = jQuery('#portfolio');
// initialize isotope
$container.isotope({
  // options...
});

// filter items when filter link is clicked
jQuery('#filters a').click(function() {
  var selector = $(this).attr('data-filter');
  $container.isotope({filter: selector});
  return false;
});

/* Scroll to Top */
jQuery(document).ready(function() {
  jQuery(".totop").hide();

  jQuery(function() {
    jQuery(window).scroll(function() {
      if (jQuery(this).scrollTop() > 600)
      {
        jQuery('.totop').slideDown();
      }
      else
      {
        jQuery('.totop').slideUp();
      }
    });

    jQuery('.totop a').click(function(e) {
      e.preventDefault();
      jQuery('body,html').animate({scrollTop: 0}, 500);
    });

  });
});

